/*
 * @Author: yuelei 
 * @Date: 2018-05-10 21:39:38 
 * @Last Modified by: Yuelei
 * @Last Modified time: 2018-05-13 16:07:05
 */
import { query,add,remove} from '../services/production';

export default {
  namespace: 'productionlist',

  state: {
    productionlists: [],
  },

  effects: {
    //   获取商品
    *query(_, { call, put }) {
      const response = yield call(query);
      console.log('test')
      console.log(response)
      console.log('test')
      yield put({
        type: 'save',
        payload: response,
      });
    },
    *remove({ payload: pro_obj }, { call, put }) {
        
      const response=  yield call(remove,pro_obj);
          yield put({
            type: "reduce",
            payload:response
          })
      
      },
    *add({ payload: params }, { call, put }) {
      const response = yield call(add, params);
      yield put({
        type: 'extend',
        payload: response,
      });
    },
  },

  reducers: {
    //   操作reducer
    save(state, action) {
      console.log('请求成功')
 
      return {
        ...state,
        
        productionlists: action.payload,
      };
    },
    extend(state, { payload: obj }) {
      const { productionlists } = state;
      return {
        ...state,
        productionlists: [obj, ...productionlists],
      };
    },
    reduce(state,  { payload: obj }) {
        const {productionlists } = state;
        // console.log('111')
        // console.log(pro_id)
        return {
          ...state,
          productionlists:[obj, ...productionlists],
        }
      },
  },
  subscriptions: {
    setup({ history, dispatch }) {
      dispatch({ type: 'query' });
    },
  },
};
